﻿using System;
using System.IO;
using System.Windows;
using WickedFlame.Core;
using WickedFlame.Util.Data;

namespace WickedFlame.Util.Actions
{
    public class ImportFromFileAction
    {
        Window _owner;

        bool _error = false;
        public bool Error
        {
            get
            {
                return _error;
            }
        }

        VectorImage _image;
        public VectorImage Image
        {
            get
            {
                return _image;
            }
        }

        static string fileDlgInitDir;

        public bool DoAction(Window owner, string fileName)
        {
            _owner = owner;

            if (string.IsNullOrEmpty(fileName))
            {
                var openFileDialog1 = new Microsoft.Win32.OpenFileDialog();
                openFileDialog1.Filter = "VecDraw files (.vec)|*.vec|Png (.png)|*.png|Jpeg (.jpg)|*.jpg|SVG (.svg)|*.svg";
                openFileDialog1.InitialDirectory = fileDlgInitDir;

                if(openFileDialog1.ShowDialog(_owner) != true)
                {
                    _error = true;
                    return false;
                }

                fileName = openFileDialog1.FileName;
                fileDlgInitDir = new FileInfo(fileName).DirectoryName;
            }

            // Read the data
            try
            {
                using (Stream stream = new FileStream(
                           fileName, FileMode.Open, FileAccess.Read))
                {
                    // Deserialize object from text format
                    FileFormat file = null;

                    switch (fileName.Substring(fileName.LastIndexOf('.')))
                    {
                        case ".png":
                            file = new PngFileFormat();
                            break;
                        case ".jpg":
                            file = new JpegFileFormat();
                            break;
                        case ".vec":
                            file = new VecFileFormat();
                            break;
                        case ".svg":
                            file = new SvgFileFormat();
                            break;
                    }

                    if (file == null)
                        throw new NotSupportedException("Fileformat is not implemented");

                    _image = new VectorImage()
                    {
                        FileName = fileName,
                        Path = fileDlgInitDir
                    };

                    Image.DrawingCanvas = file.Import(stream);
                }
            }
            // Catch all exceptions which may be raised from this code.
            // Caller is responsible to handle all other exceptions 
            // in the functions invoked by LoadEvent and DocChangedEvent.
            catch (ArgumentNullException ex) { return HandleLoadException(ex, fileName); }
            catch (ArgumentOutOfRangeException ex) { return HandleLoadException(ex, fileName); }
            catch (ArgumentException ex) { return HandleLoadException(ex, fileName); }
            catch (System.Security.SecurityException ex) { return HandleLoadException(ex, fileName); }
            catch (FileNotFoundException ex) { return HandleLoadException(ex, fileName); }
            catch (DirectoryNotFoundException ex) { return HandleLoadException(ex, fileName); }
            catch (PathTooLongException ex) { return HandleLoadException(ex, fileName); }
            catch (IOException ex) { return HandleLoadException(ex, fileName); }
            catch (NotSupportedException ex) { return HandleLoadException(ex, fileName); }
            catch (CanvasException ex) { return HandleLoadException(ex, fileName); }

            // Clear dirty bit, cache the file name and set the caption
            if (_image != null)
            {
                _image.FileName = fileName;
                _image.Path = fileDlgInitDir;
                _image.IsUnwrittenCanvas = false;
            }
            else
                _error = true;

            return _error;








            //FileType file = null;

            //try
            //{
            //    switch (fileName.Substring(fileName.LastIndexOf('.')))
            //    {
            //        case ".png":
            //            file = new PngFileType();
            //            break;
            //        case ".jpg":
            //            file = new JpegFileType();
            //            break;
            //        case ".vec":
            //            file = new VecFileType();
            //            break;
            //        default:
            //            MessageBox.Show("Fileformat is not implemented");
            //            break;
            //    }

            //    if (file != null)
            //        this.canvas = file.Import(stream);
            //}
            //catch (ArgumentNullException ex)
            //{
            //    HandleLoadException(ex, fileName);
            //}
            //catch (System.Runtime.Serialization.SerializationException ex)
            //{
            //    HandleLoadException(ex, fileName);
            //}
            //catch (System.Security.SecurityException ex)
            //{
            //    HandleLoadException(ex, fileName);
            //}
        }


        public void DoAction(Window owner, string fileName, Stream stream)
        {
            _owner = owner;

            FileFormat file = null;

            try
            {
                switch (fileName.Substring(fileName.LastIndexOf('.')))
                {
                    case ".png":
                        file = new PngFileFormat();
                        break;
                    case ".jpg":
                        file = new JpegFileFormat();
                        break;
                    case ".vec":
                        file = new VecFileFormat();
                        break;
                    default:
                        throw new NotSupportedException("Fileformat is not implemented");
                        //MessageBox.Show("Fileformat is not implemented");
                        //break;
                }

                if (file != null)
                    _image.DrawingCanvas = file.Import(stream);
            }
            catch (ArgumentNullException ex)
            {
                HandleLoadException(ex, fileName);
            }
            catch (System.Runtime.Serialization.SerializationException ex)
            {
                HandleLoadException(ex, fileName);
            }
            catch (System.Security.SecurityException ex)
            {
                HandleLoadException(ex, fileName);
            }
            catch (NotSupportedException ex)
            {
                HandleLoadException(ex, fileName);
            }
        }

        private bool HandleLoadException(Exception ex, string fileName)
        {
            MessageBox.Show(_owner,
                "Open File operation failed. File name: " + fileName + "\n" +
                "Reason: " + ex.Message,
                _owner.Title);

            _error = true;
            return _error;
        }
    }
}
